package com.jean.init;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.jean.util.LoggerFactory;

/**
 * <一句话功能简述>上下文获取 <功能详细描述>
 * 
 * @author Jean
 * @version [版本号, 2013年12月5日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public final class MusicContext
{
    /**
     * 上下文
     */
    private static ApplicationContext ctx =
        ContextLoader.getCurrentWebApplicationContext();
    
    /**
     * 日志
     */
    private static Logger logger = LoggerFactory.getLogger();
    
    /**
     * SqlMapClient
     */
    private static SqlMapClient sqlMapClient = null;
    
    /**
     * <一句话功能简述>获取SqlMapClient实例
     * <功能详细描述>
     * @return SqlMapClient实例
     * @see [类、类#方法、类#成员]
     */
    public static SqlMapClient getSqlMapClient()
    {
        sqlMapClient = ctx.getBean("sqlMapClient", SqlMapClient.class);
        if (sqlMapClient == null)
        {
            logger.warn("get sqlMapClient by context, sqlMapClient is null");
        }
        return sqlMapClient;
    }
    
    /**
     * <一句话功能简述>setter方法 <功能详细描述>
     * 
     * @param context 应用上下文
     * @see [类、类#方法、类#成员]
     */
    public void setApplicationContext(ApplicationContext context)
    {
        MusicContext.ctx = context;
    }
    
    /**
     * <一句话功能简述>获取bean配置对象 <功能详细描述>
     * 
     * @param beanName bean配置的name
     * @return bean配置对象
     * @see [类、类#方法、类#成员]
     */
    public static Object getBean(String beanName)
    {
        if (StringUtils.isBlank(beanName))
        {
            logger.warn("The bean name is blank.");
            return null;
        }
        logger.info("ctx get bean, beanname: " + beanName);
        return ctx.getBean(beanName);
    }
    
    /**
     * <一句话功能简述>获取bean配置对象 <功能详细描述>
     * 
     * @param beanName bean配置的name
     * @param clazz Class
     * @return bean配置对象
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanName, Class<T> clazz)
    {
        if (StringUtils.isBlank(beanName))
        {
            logger.warn("The bean name is blank.");
            return null;
        }
        if (clazz == null)
        {
            return (T)getBean(beanName);
        }
        logger.info("ctx get bean, beanname: " + beanName + ", class: "
            + clazz.getName());
        return ctx.getBean(beanName, clazz);
    }
    
    /**
     * <一句话功能简述>获取资源 <功能详细描述>
     * 
     * @param request 请求
     * @param key 资源key值
     * @return 资源
     * @see [类、类#方法、类#成员]
     */
    public static String getMessage(HttpServletRequest request, String key)
    {
        if (StringUtils.isBlank(key))
        {
            logger.warn("get message, the key is blank.");
            return "";
        }
        return getMessage(request, key, new String[] {});
    }
    
    /**
     * <一句话功能简述>获取资源 <功能详细描述>
     * 
     * @param request 请求
     * @param key 资源key值
     * @param params 资源参数
     * @return 资源
     * @see [类、类#方法、类#成员]
     */
    public static String getMessage(HttpServletRequest request, String key,
        String... params)
    {
        if (StringUtils.isBlank(key))
        {
            logger.warn("get message, the key is blank.");
            return "";
        }
        logger.info("get message, key: " + key + ", params: "
            + ArrayUtils.toString(params, ""));
        return ctx.getMessage(key, params, getLocale(request));
    }
    
    /**
     * <一句话功能简述>获取Locale国际化 <功能详细描述>
     * 
     * @param request 请求
     * @return Locale对象
     * @see [类、类#方法、类#成员]
     */
    public static Locale getLocale(HttpServletRequest request)
    {
        Locale locale =
            (Locale)request.getAttribute(CookieLocaleResolver.LOCALE_REQUEST_ATTRIBUTE_NAME);
        if (locale == null)
        {
            logger.info("get locale by request.attribute is null.");
            CookieLocaleResolver resolver =
                getBean("localeResolver", CookieLocaleResolver.class);
            resolver =
                resolver != null ? resolver
                    : (CookieLocaleResolver)RequestContextUtils.getLocaleResolver(request);
            locale = resolver.resolveLocale(request);
        }
        if (locale == null)
        {
            logger.info("get locale by Locale.getDefault().");
            locale = Locale.getDefault();
        }
        return locale;
    }
}
